package org.ruanjj.streamapi;

import org.junit.Test;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.IntStream;
import java.util.stream.Stream;

public class Demo {
    //1. List 进行创建，可以创建串行流也可以创建并行流
    @Test
    public void test1(){
        List<User1> users = List.of(
                new User1("Mary",30),
                new User1("Tom",56),
                new User1("Tony",29),
                new User1("Sisi",12),
                new User1("Dany",5)
        );

        List<String> names = users.stream()
                .map(User1::name)
                .collect(Collectors.toList());
        names.forEach(System.out::println);

        System.out.println("====================================");

        List<Integer> ages  = users.stream()
                .map(User1::age)
                .sorted((o1, o2) -> o2-o1)
                .collect(Collectors.toList());
        ages.forEach(System.out::println);

    }

    //2.进行数组创建
    @Test
    public void test2(){
        IntStream stream = Arrays.stream(new int[]{6,3,7,4,2,5,1,9});
        int sum = Arrays.stream(new int[]{6,3,7,4,2,5,1,9}).reduce(0,Integer::sum);
        List<Integer> a = Arrays.stream(new int[]{6,3,7,4,2,5,1,9}).sorted().boxed().collect(Collectors.toList());
        System.out.println(sum);
    }

    @Test
    public void test3(){
        List<Integer> list = Stream.of(2,5,6,7,1,3,5,9,0).distinct().collect(Collectors.toList());
        list.forEach(System.out::println);

        System.out.println(list.stream().reduce(0, Integer::sum));

        System.out.println(list.stream().max(Integer::compareTo).get());

    }
}
